package com.hr.controller;

import com.hr.dto.Page;
import com.hr.dto.Result;
import com.hr.entity.Role;
import com.hr.enums.ExceptionEnum;
import com.hr.exception.MyException;
import com.hr.service.RoleService;
import com.hr.vo.AuthVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Api(tags = "角色管理")
@CrossOrigin
@RestController
public class RoleController {

    @Resource
    private RoleService roleService;

    @ApiOperation(value = "新增角色")
    @PostMapping("/addRole")
    public Result addRole(@ApiParam(value = "角色对象", required = true) @RequestBody Role role){
        try{
            roleService.addRole(role);
            return Result.ok();
        }catch (Exception e){
            throw new MyException(ExceptionEnum.ROLE_ADD_ERROR);
        }
    }

    @ApiOperation(value = "删除角色")
    @GetMapping("/deleteRole")
    public Result deleteRole(@ApiParam(value = "角色id", required = true) @RequestParam("roleId") Integer roleId){
        try{
            roleService.deleteRole(roleId);
            return Result.ok();
        }catch (Exception e){
            throw new MyException(ExceptionEnum.ROLE_DELETE_ERROR);
        }
    }

    @ApiOperation(value = "更新角色")
    @PostMapping("/updateRole")
    public Result updateRole(@ApiParam(value = "角色对象", required = true) @RequestBody Role role){
        try{
            roleService.updateRole(role);
            return Result.ok();
        }catch (Exception e){
            throw new MyException(ExceptionEnum.ROLE_UPDATE_ERROR);
        }
    }

    @ApiOperation(value = "查询角色")
    @GetMapping("/getRole")
    public Result getRole(@ApiParam(value = "角色id", required = true) @RequestParam("roleId") Integer roleId){
        try {
            Role role = roleService.getRole(roleId);
            return Result.ok(role);
        }catch (Exception e){
            throw new MyException(ExceptionEnum.ROLE_SELECT_ERROR);
        }
    }

    @ApiOperation(value = "分页查询角色列表")
    @GetMapping("/getRoleList")
    public Result getRoleList(@ApiParam(value = "分页编号", required = true) @RequestParam("pageNo") int pageNo,
                              @ApiParam(value = "显示条数", required = true) @RequestParam("pageSize") int pageSize){
        try{
            Page page = roleService.getRolePage(pageNo, pageSize);
            return Result.ok(page);
        }catch (Exception e){
            throw new MyException(ExceptionEnum.ROLE_SELECTLIST_ERROR);
        }
    }

    @ApiOperation("查询所有可用角色")
    @GetMapping("/getAllRole")
   public Result getAllRole(){
        return Result.ok(roleService.getAllRole());
   }

    @ApiOperation(value = "给角色授权")
    @PostMapping("/auth")
    public Result auth(@ApiParam(value = "AuthVO，授权对象", required = true) @RequestBody AuthVO authVO){
        try{
            roleService.auth(authVO);
            return Result.ok();
        }catch (Exception e){
            throw new MyException(ExceptionEnum.ROLE_AUTH_ERROR);
        }
    }

}
